package com.jason.seckill.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.jason.seckill.pojo.SeckillMessage;
import com.jason.seckill.pojo.User;
import com.jason.seckill.service.IGoodsService;
import com.jason.seckill.service.IOrderService;
import com.jason.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MQReceiver {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;
    //正常下单操作 还是需要判断库存 而不是前面redis暂存那边
    @RabbitListener(queues = "seckillQueue")
    public void receiveSeckillMessage(String message){
        log.info("接收到消息:"+ message);
        SeckillMessage seckillMessage = JSON.parseObject(message, SeckillMessage.class);
        Long goodId = seckillMessage.getGoodId();
        User user = seckillMessage.getUser();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodId);
        if (goodsVo.getStockCount()<1){
             return;
        }
        orderService.seckill(user,goodsVo);
    }
}
